﻿package nl.sanity.net 
{
	import flash.net.SharedObject;
	
	/**
	 * Cookie
	 * Value storage & retrieve class for SharedObjects 
	 * 
	 * @langversion ActionScript 3
	 *
	 * @author M.P. Bartnig
	 * @since  28/03/2009
	 */
	public final class Cookie
	{
		public static const DEFAULT_NAME	:String = "FlashCookie";
	
		protected var cookie				:SharedObject;
		protected var _name					:String;
		
		// --- CONSTRUCTOR ----------------------------------------

		public function Cookie( name:String = null ) 
		{
			_name	= name || Cookie.DEFAULT_NAME;
			cookie	= SharedObject.getLocal( _name, "/" );
		}
		
		// --- PUBLIC FUNCTIONS ----------------------------------------

		public function read():Object
		{
			return cookie.data;
		}
		
		public function write( props:Object ):void
		{
			for ( var i:* in props )
			{
				cookie.data[ i ] = props[ i ]
			}
			cookie.flush();
		}
		
		public function getValue( key:String ):String
		{
			return cookie.data[ key ] || null;
		}
		
		public function storeValue( key:String, value:String ):void
		{
			cookie.data[ key ] = value;
			cookie.flush();
		}
		
		// --- GETTERS/SETTERS ----------------------------------------

		public function get name():String
		{
			return _name;
		}
		
		public function set name( value:String ):void 
		{
			_name = value;
		}	
	}
	
}